<template>
  <div>
    <el-form ref="form" :model="form" label-width="100px">
      <el-row style="border-bottom: 1.5px solid #f4f6f8;">
        <el-row>
          <el-col :span="6">
            <div class="grid-content bg-purple">
              <el-form-item label="一体式名称：">
                <el-input v-model="form.poleName" placeholder="请输入一体式名称" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple-light">
              <el-form-item label="一体式编号：">
                <el-input v-model="form.poleNumber" placeholder="请输入一体式编号" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">
              <el-form-item label="运行状态：">
                <el-input v-model="form.poleStatus" placeholder="请输入运行状态" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="6" style="margin-left: -80px !important;">
            <div class="grid-content bg-purple-light"><el-form-item>
              <el-button type="primary" @click.prevent="List">查询</el-button>
            </el-form-item></div>
          </el-col>
        </el-row>
      </el-row>
    </el-form>

    <!-- 按钮 -->
    <div>
      <el-row style="margin-top: 10px;">
        <el-button type="primary" @click="open('add')">添加一体杆</el-button>
        <el-button @click="delA">批量删除</el-button>
      </el-row>
    </div>

    <!-- 列表 -->
    <div>
      <el-row>
        <el-table style="width: 100%" :data="tableData" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column type="index" label="序号" width="100" />
          <!--  -->
          <el-table-column prop="poleName" label="一杆式名称" />
          <el-table-column prop="poleIp" label="一杆式IP" />
          <el-table-column prop="areaName" label="安装区域" />
          <el-table-column prop="poleType" label="一杆式类型">
            <template #default="{ row }">
              {{ poleTypeLisy(row.poleType) }}
            </template>
          </el-table-column>
          <el-table-column prop="poleStatus" label="运行状态" width="180">
            <template #default="{ row }">
              {{ row.poleStatus === 0 ? '正常' : '异常' }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-link type="primary" @click="editOpen('edit', row)"> 编辑</el-link>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <el-link type="primary" @click="del(row.id)">删除</el-link>
            </template>

          </el-table-column>

        </el-table>
      </el-row>
      <div class="block">
        <el-pagination
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="form.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog :title="title" :visible="dialogVisible" width="480px" @close="closeDialog">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="100px">
          <el-form-item label="一杆式名称" prop="poleName">
            <el-input v-model="addForm.poleName" style="width: 320px" />
          </el-form-item>
          <el-form-item label="一杆式编号" prop="poleNumber">
            <el-input v-model="addForm.poleNumber" style="width: 320px" />
          </el-form-item>
          <el-form-item label="一体式IP" prop="poleIp">
            <el-input v-model="addForm.poleIp" style="width: 320px" />
          </el-form-item>
          <el-form-item label="关联地域" prop="areaId">
            <el-select v-model="addForm.areaId" placeholder="请选择角色" style="width: 320px">
              <el-option v-for="item in areaList2" :key="item.id" :value="item.id" :label="item.areaName" />
            </el-select>
          </el-form-item>
          <el-form-item label="一杆式类型" prop="poleType">
            <el-select v-model="addForm.poleType" placeholder="请选择角色" style="width: 320px">
              <el-option
                v-for="item in poleTypeList2"
                :key="item.id"
                :value="item.areaM"
                :label="item.areaName"
              /></el-select>
          </el-form-item>

        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="closeDialog">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getList, delList, addList, editList } from '@/api/rodManage'
import { areaList, poleTypeList } from '@/api/rodManageList'
export default {
  name: 'RodManagePage',
  data() {
    return {
      type: '',
      areaList2: [],
      poleTypeList2: [],
      tableData: [],
      form: {
        pageSize: 10,
        page: 1,
        poleName: '',
        poleNumber: '',
        poleStatus: ''
      },
      total: 0,
      addForm: {
        id: '',
        areaId: '',
        poleIp: '',
        poleName: '',
        poleNumber: '',
        poleType: ''
      },
      addFormRules: {
        areaId: [
          { required: true, message: '请输入名字', trigger: 'blur' }
        ],
        poleIp: [
          { required: true, message: '请输入名字', trigger: 'blur' }
        ],
        poleName: [
          { required: true, message: '请输入名字', trigger: 'blur' }
        ],
        poleNumber: [
          { required: true, message: '请输入名字', trigger: 'blur' }
        ],
        poleType: [
          { required: true, message: '请输入名字', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      ids: []
    }
  },
  computed: {
    title() {
      if (this.type === 'add') {
        return '添加'
      } else {
        return '编辑'
      }
    }
  },
  created() {
    this.area()
    this.List()
  },
  methods: {
    handleSelectionChange(val) {
      console.log(val)
      this.ids = val.map(item => item.id)
    },
    // 地域
    area() {
      this.areaList2.push(...areaList())
      this.poleTypeList2.push(...poleTypeList())
    },
    // 点击点击确定
    async submit() {
      this.$refs.addForm.validate(async(valid) => {
        if (!valid) return
        if (this.type === 'add') {
          await addList(this.addForm)
          this.$message.success('添加成功')
        } else {
          await editList(this.addForm)
          this.$message.success('编辑成功')
        }
        this.dialogVisible = false
        this.$refs.addForm.resetFields()
        this.List()
      })
    },
    // 点击打开
    async open(type) {
      this.type = type
      this.dialogVisible = true
    },
    editOpen(type, row) {
      // console.log(row)
      this.type = type
      this.dialogVisible = true
      this.addForm = row
    },
    // 点击取消
    closeDialog() {
      this.dialogVisible = false
      this.$refs.addForm.resetFields()
    },
    async List() {
      const res = await getList(this.form)
      // console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    // 分页
    handleSizeChange(val) {
      // console.log(val)
      this.form.pageSize = val
      this.List()
    },
    handleCurrentChange(val) {
      this.form.page = val
      this.List()
    },
    // 一杆式类型
    poleTypeLisy(poleType) {
      const a = {
        entrance: '入口',
        export: '出口',
        center: '中间'
      }
      return a[poleType]
    },
    // 删除按钮
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delList(id, ...this.ids)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.List()
      })
    },
    // 删除按钮
    delA() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delList(this.ids)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.List()
      })
    }

  }

}
</script>
<style scoped  lang="scss">
.block {
  text-align: right;
}
</style>

